package benny.ApkposStore.Adapter;

import java.lang.reflect.Field;
import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import benny.ApkposStore.R;

public class imageplayAdapter extends BaseAdapter {

	private Context mcontext;
	private ArrayList<Integer> residList = new ArrayList<Integer>(); // 通过放射机制保存所有图片的id

	public imageplayAdapter(Context context) {
		mcontext = context;
		// 反射的可重用性更好
		// R.id在R文件中本质上是一个类，我们通过这个R.id.class.getClass().getDeclaredFields()可以找到它的所有属性
		Field[] residFields = R.drawable.class.getDeclaredFields();
		for (Field residField : residFields) {
			// 例如： public static final int icon=0x7f020000;
			// 它的Field表示为 : name= icon ; field.getInt() = 0x7f020000
			if (residField.getName().startsWith("keyb")) {
				int resid;
				try {
					resid = residField.getInt(null);// 找到该属性的值
					residList.add(resid);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public int getCount() {
		return residList.size();
	}

	@Override
	public Object getItem(int position) {
		return residList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
    //�õ���ͼƬ��res id
	public int getResId(int position) {
		return residList.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView img;
		if (convertView == null) {
			img = new ImageView(mcontext);
			img.setScaleType(ImageView.ScaleType.FIT_XY);
			img.setLayoutParams(new Gallery.LayoutParams(100, 100));  // 图片显示宽和长
			img.setImageResource(residList.get(position));
		} else {
			img = (ImageView) convertView;
		}
		return img;
	}

}